package com.cetc39.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.cetc39.bean.FirstPlan;
import com.cetc39.dao.FirstplanDao;
import com.cetc39.dao.ProjectDao;
import com.cetc39.service.FirstplanService;

public class FirstplanServiceImpl implements FirstplanService {

	private FirstplanDao firstplanDao;
	
	@Override
	public void add(FirstPlan fp) {
		firstplanDao.save(fp);
	}

	@Override
	public void delete(int id) {
		firstplanDao.delete(id);
	}

	@Override
	public void update(FirstPlan fp) {
		firstplanDao.update(fp);
	}

	@Override
	public List<?> findAll() {
		return firstplanDao.findAll();
	}

	@Override
	public FirstPlan findById(int id) {
		return firstplanDao.findById(id);
	}

	public void setFirstplanDao(FirstplanDao firstplanDao) {
		this.firstplanDao = firstplanDao;
	}

	@Override
	public List<?> findByProjectId(int id) {
		return firstplanDao.findByProjectId(id);
	}

	@Override
	public void release(int plan_id) {
		firstplanDao.release(plan_id);
	}

	@Override
	public List<?> findReleasedPlan() {
		List<?> releasedPlan = firstplanDao.findReleasedPlan();
		return releasedPlan;
	}

	@Override
	public List<?> findReleasedPlanByCondition(String[] conditions) {
		List<?> releasedPlanByCondition = firstplanDao.findReleasedPlanByCondition(conditions);
		return releasedPlanByCondition;
	}

	@Override
	public void access_plan(int plan_id) {
		firstplanDao.access_plan(plan_id);
	}

	@Override
	public void reject_plan(int plan_id) {
		firstplanDao.reject_plan(plan_id);
	}

	@Override
	public List<?> findReviewdPlan(int userID, int roleID) {
		List<?> firstplanList = firstplanDao.findReviewdPlan();
		List<FirstPlan> finalList = new ArrayList<FirstPlan>();
		if(roleID == 2) {
			for(Object o : firstplanList) {
				FirstPlan fp = (FirstPlan) o;
				if(fp.getProject().getCpd_ps().getId() == userID) {
					finalList.add(fp);
				}
			}
		} else if(roleID == 3) {
			for(Object o : firstplanList) {
				FirstPlan fp = (FirstPlan) o;
				if(fp.getProject().getProject_manager().getId() == userID) {
					finalList.add(fp);
				}
			}
		}
		return finalList;
	}

	@Override
	public List<?> findPlanNeedToApproval(int userID, int roleID) {
		List<?> firstplanList = firstplanDao.findAll();
		List<FirstPlan> finalList = new ArrayList<FirstPlan>();
		if(roleID == 2) {
			for(Object o : firstplanList) {
				FirstPlan fp = (FirstPlan) o;
				if(fp.getProject().getCpd_ps().getId() == userID && fp.getPlan_release() == 2) {
					finalList.add(fp);
				}
			}
		} else if(roleID == 3) {
			for(Object o : firstplanList) {
				FirstPlan fp = (FirstPlan) o;
				if(fp.getProject().getProject_manager().getId() == userID && fp.getPlan_release() == 2) {
					finalList.add(fp);
				}
			}
		}
		return finalList;
	}


	
	
}
